unit MMDBRegistry;

interface

uses Registry, Dialogs, Sysutils, Forms, Graphics, WinAPI.Windows;

type TdbReg = class
  public
    DocCtrlPath: string;   //path to document control
    KitsPath: string;   //path to doc control Kits directory
    NetCDSPath: string;      //path to master data set file
    LocalCDSPath: string;   //path to local copy of data set file
    LogFilePath:  string;    //path to log file.
    procedure RegLoad;
    procedure RegSave;
end;

implementation

(***************************************************************************
RegLoad(): Load registry values into dbReg object for use throughout the
           session.
*******************************************************************************)
procedure TdbReg.RegLoad;
var
  RegTemp: TRegistry;
  str: string;
begin
  RegTemp := TRegistry.Create; //Create registry object
try
  with RegTemp do
  begin
    if not (KeyExists('Software\Merit\MMDBV2UPDATERV2')) then //See if key exists
      OpenKey('Software\Merit\MMDBV2UPDATERV2',True) //Create and open  key
      else
      OpenKey('Software\Merit\MMDBV2UPDATERV2', False); //Sets current key
      {Create and initialize values if they do not exist.}
      if not (ValueExists('VERSION')) then
        begin
         WriteString('VERSION','V2.0');  //allows registry to be updated when
                                        //new versions are released.
         WriteString('DOCCTRLPATH', '\\merit.com\sj_doc\DOC');
         WriteString('KITSPATH', '\\merit.com\sj_doc\KITS\DCKITS');
         WriteString('NETCDSPATH', '\\merit.com\sj_public\PUBLIC\DocBrowser\MMDBV2_1');
         WriteString('LOCALCDSPATH', ExtractFilePath(Application.ExeName));
         WriteString('LOGFILEPATH', ExtractFilePath(Application.ExeName));
       end
        else
         begin
          if not (ValueExists('VERSION')) then
           WriteString('VERSION','V2.0');
          if not (ValueExists('DOCCTRLPATH')) then
           WriteString('DOCCTRLPATH', '\\merit.com\sj_doc\DOC');
          if not (ValueExists('KITSPATH')) then
           WriteString('KITSPATH', '\\merit.com\sj_doc\KITS\DCKITS');
          if not (ValueExists('NETCDSPATH')) then
            WriteString('NETCDSPATH', '\\merit.com\sj_public\PUBLIC\DocBrowser\MMDBV2_1');
          if not (ValueExists('LOCALCDSPATH')) then
           WriteString('LOCALCDSPATH', ExtractFilePath(Application.ExeName));
          if not (ValueExists('LOGFILEPATH')) then
           WriteString('LOGFILEPATH', ExtractFilePath(Application.ExeName));
         end;
      {Initialize TdbReg class member values from registry.}
      DocCtrlPath := ReadString('DOCCTRLPATH');
      KitsPath := ReadString('KITSPATH');
      NetCDSPath := ReadString('NETCDSPATH');
      LocalCDSPath := ReadString('LOCALCDSPATH');
      LogFilePath := ReadString('LOGFILEPATH');
  end;     //end with statement
  except
    //ShowMessage('Unable to access registry. Switching' +#13 +
    //            'to default values.');
    DocCtrlPath := '\\merit.com\sj_doc\DOC';
    KitsPath := '\\merit.com\sj_doc\KITS\DCKITS';
    NetCDSPath := '\\merit.com\sj_public\PUBLIC\DocBrowser\MMDBV2_1';
    LocalCDSPath := ExtractFilePath(Application.ExeName);
    LogFilePath := ExtractFilePath(Application.ExeName);
    //May need to add Font info here, but assume app defaults will be used.
  end;
  RegTemp.Free;
end;

(***************************************************************************
RegSave(): Save dbReg values back to the registry.
*******************************************************************************)
procedure TdbReg.RegSave;
var
  RegTemp: TRegistry;
begin
  RegTemp := TRegistry.Create; //Create registry object
  with RegTemp do
  begin
   try
     OpenKey('Software\Merit\MMDBV2UPDATERV2', False); //Sets current key
     WriteString('DOCCTRLPATH', DocCtrlPath);
     WriteString('KITSPATH', KitsPath);
     WriteString('NETCDSPATH', NetCDSPath);
     WriteString('LOCALCDSPATH', LocalCDSPath);
     WriteString('LOGFILEPATH', LogFilePath);
   except
    ShowMessage('Unable to save values to registry. '+#13 +
                'Default values will be loaded on next run.');
   end;
  end;   //end with
  RegTemp.Free;
end;


end.

